#pragma once
#include "sclip.h"
#include "templatearray.h"

class SFrame
{
private:
	/** component clips that make up a frame */
	TemplateArray<SClip> m_clips;
	/** how long this frame should display */
	Uint32 m_duration;
	/** how much space this frame of animation takes */
	SRect m_dimension;
	static const int DEFAULT_DURATION = 100;
public:
	inline Uint32 getDelay(){return m_duration;}
	inline void release(){m_clips.release();}
	SFrame():m_duration(DEFAULT_DURATION){}
	/** copy method */
	void copy(const SFrame & sf)
	{
		release();
		m_clips.copy(sf.m_clips);
		m_duration=sf.m_duration;
		m_dimension.set(sf.m_dimension);
	}
	/** copy constructor */
	SFrame(const SFrame & sf){copy(sf);}
	/** must overload the assignment operator to work with custom templated data structs */
	SFrame & operator=(const SFrame & sf){copy(sf);return*this;}
	/** @param a_duration how many milliseconds this frame should display during animation */
	inline void setDuration(const int & a_duration){m_duration=a_duration;}
	void setClipCount(const int & a_clipCount)
	{
		release();
		m_clips.allocateToSize(a_clipCount);
	}
	inline int getClipCount()const{return m_clips.size();}
	inline SClip * getClip(const int & a_index)const{return &m_clips.get(a_index);}
	inline TemplateArray<SClip> * getClips(){return &m_clips;}
	~SFrame(){release();}
	inline void invalidateFrameSize(){
		m_dimension.set(0,0,0,0);
	}
	SRect * getFrameSize()
	{
		int clips = getClipCount();
		if(!m_dimension.isValid() && clips > 0)
		{
			m_dimension.set(getClip(0)->getRect());
			for(int i = 1; i < clips; ++i)
				m_dimension.add(getClip(i)->getRect());
		}
		return &m_dimension;
	}
	void draw(SDL_Surface * a_screen, const SPoint & a_center, const int & a_flip, const int & a_zoom) const
	{
		for(int i = 0; i < getClipCount(); ++i)
			getClip(i)->draw(a_screen, a_center, a_flip, a_zoom);
	}
};
